package com.wxzz.elearing.evaluation.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.evaluation.dto.*;
import com.wxzz.elearing.evaluation.entity.*;
import com.wxzz.elearing.evaluation.mapper.BusinessCategoryMapper;
import com.wxzz.elearing.evaluation.service.IBusinessCategoryEvaluationService;
import com.wxzz.elearing.evaluation.service.IBusinessCategoryRuleService;
import com.wxzz.elearing.evaluation.service.ITopicService;
import com.wxzz.elearing.evaluation.service.openfeign.IEnterpriseApiFeign;
import com.wxzz.elearning.common.utils.OpenFeignUtil;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 能力测评 前端控制器
 * </p>
 *
 * @author
 * @since 2021-04-10
 */
@RestController
@RequestMapping("/business-category-evaluation")
@Api(tags = "能力测评接口(总后台)")
public class BusinessCategoryEvaluationController {

    @Autowired
    private IBusinessCategoryRuleService businessCategoryRuleService;

    @Autowired
    private IBusinessCategoryEvaluationService businessCategoryEvaluationService;

    @ApiOperation(value = "能力测评添加题库(总后台)", notes = "能力测评添加题库(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveBusinessCategoryTopic4evaluation", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryTopic4evaluation(@Valid @RequestBody BusinessCategoryTopicInsDto businessCategoryTopicInsDto) {
        return businessCategoryRuleService.saveBusinessCategoryTopic(2, businessCategoryTopicInsDto);
    }

    @ApiOperation(value = " 添加能力测评(总后台)", notes = " 添加能力测评(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveBusinessCategoryEvaluation", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryEvaluation(@RequestBody BusinessCategoryEvaluation businessCategoryEvaluation) {
        return businessCategoryEvaluationService.saveBusinessCategoryEvaluation(businessCategoryEvaluation);
    }

    @ApiOperation(value = "根据id删除能力测评(总后台)", notes = "根据id删除能力测评(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "deleteBusinessCategoryEvaluationId/{businessCategoryEvaluationId}", method = RequestMethod.POST)
    public ReturnVO deleteBusinessCategoryEvaluationId(@PathVariable Long businessCategoryEvaluationId) {
        return businessCategoryEvaluationService.deleteBusinessCategoryEvaluationId(businessCategoryEvaluationId);
    }

    @ApiOperation(value = " 能力测评列表(总后台)", notes = " 能力测评列表(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryEvaluation", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryEvaluation>>> listBusinessCategoryEvaluation(@RequestHeader String token,@RequestBody BusinessCategoryEvaluationDto businessCategoryEvaluationDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryEvaluationService.listBusinessCategoryEvaluation(null,businessCategoryEvaluationDto);
    }

    @Autowired
    private BusinessCategoryMapper businessCategoryMapper;

    @Autowired
    private IEnterpriseApiFeign enterpriseApiFeign;

    @ApiOperation(value = "企业能力测评列表(后台)", notes = "企业能力测评列表(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listBusinessCategoryEvaluation4enterprise", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryEvaluation>>> listBusinessCategoryEvaluation4enterprise(@RequestHeader String token, @RequestBody BusinessCategoryEvaluationDto businessCategoryEvaluationDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        ReturnVO<String> stringReturnVO = enterpriseApiFeign.selectEnterpriseById(jsonObject.getLong("enterpriseId"));
        JSONObject returnJsonObject = OpenFeignUtil.getJSONObject(stringReturnVO);
        businessCategoryEvaluationDto.setBusinessCategoryId(returnJsonObject.getLong("businessCategoryId"));
        return businessCategoryEvaluationService.listBusinessCategoryEvaluation(jsonObject.getLong("enterpriseId"),businessCategoryEvaluationDto);
    }

    @Autowired
    private ITopicService topicService;

    @ApiOperation(value = " 能力测评详情题库(总后台)", notes = " 能力测评详情题库(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listInTopic4evaluation", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Topic>>> listInTopic4evaluation(@RequestBody InTopicDto inTopicDto) {
        return topicService.listInTopic(inTopicDto);
    }

    @ApiOperation(value = " 列出能力测评记录(总后台)", notes = " 列出能力测评记录(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryEvaluationHistory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryEvaluationHistory>>> listBusinessCategoryEvaluationHistory(@RequestBody BusinessCategoryEvaluationHistoryDto businessCategoryEvaluationHistoryDto) {
        return businessCategoryEvaluationService.listBusinessCategoryEvaluationHistory(businessCategoryEvaluationHistoryDto);
    }

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "企业列出能力测评记录(总后台)", notes = "企业列出能力测评记录(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryEvaluationHistory4enterprise", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryEvaluationHistory>>> listBusinessCategoryEvaluationHistory4enterprise(@RequestHeader String token,@RequestBody BusinessCategoryEvaluationHistoryDto businessCategoryEvaluationHistoryDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        businessCategoryEvaluationHistoryDto.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return businessCategoryEvaluationService.listBusinessCategoryEvaluationHistory(businessCategoryEvaluationHistoryDto);
    }

    @ApiOperation(value = " 列出能力测评记录试题(总后台)", notes = " 列出能力测评记录试题(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listTopicHistory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<TopicHistory>>> listTopicHistory(@RequestBody TopicHistoryDto topicHistoryDto) {
        topicHistoryDto.setBusinessType(2);
        return businessCategoryEvaluationService.listTopicHistory(topicHistoryDto);
    }

    @ApiOperation(value = "分配能力测评人员(后台)", notes = "分配能力测评人员(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveWxUserBusinessCategoryGroup", method = RequestMethod.POST)
    public ReturnVO saveWxUserBusinessCategoryGroup(@RequestHeader String token,@RequestBody WxUserBusinessCategoryEvaluationDto wxUserBusinessCategoryEvaluationDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryEvaluationService.saveWxUserBusinessCategoryGroup(jsonObject.getLong("enterpriseId"),wxUserBusinessCategoryEvaluationDto);
    }

    @ApiOperation(value = "查询能力测评的绑定人员(后台)", notes = "查询能力测评的绑定人员(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectWxUserBusinessCategoryEvaluationBind", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserBusinessCategoryGroup>>> selectWxUserBusinessCategoryEvaluationBind(@RequestHeader String token,@RequestBody WxUserBusinessCategoryEvaluationBindDto wxUserBusinessCategoryEvaluationBindDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryEvaluationService.selectWxUserBusinessCategoryEvaluationBind(jsonObject.getLong("enterpriseId"),wxUserBusinessCategoryEvaluationBindDto);
    }

}
